package dejava.bolsadetrabajo.repository.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import dejava.bolsadetrabajo.repository.AvisoPostulanteRepository;
import dejava.bolsadetrabajo.repository.model.AvisoPostulante;

public class AvisoPostulanteRepositoryJdbc implements AvisoPostulanteRepository {

	@Override
	public void registrarAvisoPostulante(AvisoPostulante oAvisoPostulante)
			throws SQLException {

		String sql = "";
		sql += "INSERT INTO bolsatrabajo.aviso_postulante";
		sql += "(idAviso,idPostulante,fechaRegistro)";
		sql += "VALUES";
		sql += "(?,?,?)";

		Connection conn = null;
		PreparedStatement ps = null;

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306",
					"root", "ojbo");

			ps = conn.prepareStatement(sql);
			ps.setInt(1, oAvisoPostulante.getIdAviso());
			ps.setInt(2, oAvisoPostulante.getIdPostulante());
			ps.setDate(3, oAvisoPostulante.getFechaRegistro());
			ps.executeUpdate();

		} catch (Exception except) {
			SQLException ex = new SQLException(
					"Error Establishing Connection: " + except.getMessage());
			throw ex;
		} finally {

			if (ps != null) {
				try {
					// Close to prevent database cursor exhaustion
					ps.close();
				} catch (SQLException ex) {
				}
			}
			if (conn != null) {
				try {
					// Close to prevent database connection exhaustion
					conn.close();
				} catch (SQLException ex) {
				}
			}
		}
	}

}
